home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 May: Tool Chest / Dev.CD May 98 TC.toast / Tool Chest / Testing & Debugging / General tools / Audit app & dcmd / Src / DisplayAuditMain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-17  |  14.2 KB  |  572 lines  |  [TEXT/KAHL]

  1. /*                                    DisplayAudit.c                                */
  2. /*
  3.  * DisplayAudit.c
  4.  * Copyright © 1992-93, Apple Computer Inc. All Rights Reserved.
  5.  * Programmed by Martin Minow,
  6.  *    Internet:    minow@apple.com
  7.  *    AppleLink:    MINOW
  8.  *
  9.  * Edit History
  10.  *    93.01.09 MM        First public release
  11.  *    93.01.14 MM        Removed Think C stdio dependencies. Added Standard File dialog
  12.  *                    with additional dialog items to allow defining the audit
  13.  *                    selector.
  14.  *    93.01.19 MM        MPW \r is the wrong character. Force <return> on both MPW and
  15.  *                    Think, because this is what the Mac file system requires for
  16.  *                    end of line. (Change is in DisplayAuditFile.c). #if'ed out
  17.  *                    some test calls. Also added error status logging for file I/O
  18.  *                    errors.
  19.  *    93.01.22 MM        Added a preference file that remembers the last audit selector
  20.  *                    parameter and window sizes. Also added error alerts. Moved
  21.  *                    global parameters to DisplayAudit.h.
  22.  *    93.07.09 MM        Reformatted text. Many other changes:
  23.  *                    -- Removed "SimpleTextWindow" -- it was far too simple,
  24.  *                    replacing it with the Log Manager from DTS Catalog Peek.
  25.  *                    -- Added "number of lines to save" to the file dialog (and
  26.  *                    wherever else necessary). Also added number of audit records
  27.  *                    to create.
  28.  *                    -- Revised overall structure so that we can (eventually)
  29.  *                    support multiple Audit records.
  30.  *                    -- Removed fixed "number of audit cycles," replacing it with
  31.  *                    the number of audit records we've created.
  32.  *                    -- Write parameter record whenever it changes. This includes
  33.  *                    changes in the window shape. Note: the parameter record will
  34.  *                    reflect the last window you created. If you create two, only
  35.  *                    the second will be in the parameter record. Actually, the
  36.  *                    "mainline" has been extensively rewritten.
  37.  *    93.08.10 MM        Merged in the new log manager (adding Print)
  38.  *
  39.  * This stand-alone application displays the data from a AuditRecord, writing it
  40.  * to a file or the screen. It is not very intelligent.
  41.  */
  42. #define EXTERN        /* */
  43. #include "DisplayAudit.h"
  44.  
  45. void                                ProcessAudit(void);
  46. void                                DisplayLogEntry(void);
  47. void                                DumpAuditEntry(
  48.         register AuditPtr                auditPtr,
  49.         register AuditEntryPtr            entryPtr
  50.     );
  51.  
  52. void                                InitMacintosh(void);
  53. void                                InitApplication(void);
  54. void                                EventLoop(void);
  55. void                                ProcessAudit(void);
  56. void                                DoMouseEvent(void);
  57. void                                DoCommand(
  58.         WindowPtr                        theWindow,
  59.         long                            menuChoice
  60.     );
  61. void                                AdjustMenus(void);
  62. void                                EnableEditMenu(void);
  63. void                                DeleteAllWindows(void);
  64. void                                DoPageSetup(void);
  65. void                                DoAbout(void);
  66.  
  67. void
  68. main(void)
  69. {
  70.         InitMacintosh();
  71.         gDebugOnError = (Get1Resource('CODE', 0) == NULL);    /* Think C hack        */
  72.         TRY {
  73.             InitApplication();
  74.              MakeDocumentWindow();
  75.             while (gQuitNow == FALSE) {
  76.                 if (gUpdateMenusNeeded) {
  77.                     gUpdateMenusNeeded = FALSE;
  78.                     AdjustMenus();
  79.                 }
  80.                 EventLoop();
  81.                 ProcessAudit();
  82.                 if (gParameterUpdateNeeded)
  83.                     WriteAuditParameters();
  84.             }
  85.         }
  86.         CATCH {
  87.             ErrorAlert(STATUS, MESSAGE, TRUE);    /* Fatal                        */
  88.             NO_PROPAGATE;                        /* Nothing to propagate to!        */
  89.         }
  90.         ENDTRY;
  91.         DeleteAllWindows();
  92.         if (gParameterFileRefNum != kNoParamFile) {
  93.             CloseResFile(gParameterFileRefNum);
  94.             gParameterFileRefNum = kNoParamFile;
  95.         }
  96.         ExitToShell();
  97. }
  98.  
  99. /*
  100.  * EventLoop runs the "main" event loop. It calls subroutines for anything
  101.  * of particular interest, such as mouse-clicks.
  102.  */
  103. void
  104. EventLoop(void)
  105. {
  106.         long                        menuChoice;
  107.         register WindowPtr            theWindow;
  108.         GrafPtr                        savePort;
  109.         Boolean                        isActivating;
  110.         
  111.         WaitNextEvent(
  112.             everyEvent,
  113.             &EVENT,
  114.             (gInForeground) ? kForegroundTicks : kBackgroundTicks,
  115.             NULL
  116.         );
  117.         theWindow = FrontWindow();
  118.         switch (EVENT.what) {
  119.         case nullEvent:
  120.             if (IsOurWindow(theWindow))
  121.                 DoWindowNullEvent((DocumentPtr) theWindow);
  122.             break;
  123.         case keyDown:
  124.         case autoKey:
  125.             if ((EVENT.message & charCodeMask) == '.'
  126.              && (EVENT.modifiers & cmdKey) != 0) {
  127.                 FlushEvents(keyDown | autoKey, 0);
  128.                 gQuitNow = TRUE;
  129.             }
  130.             else if ((EVENT.modifiers & cmdKey) != 0) {
  131.                 if (EVENT.what == keyDown) {
  132.                     menuChoice = MenuKey(EVENT.message & charCodeMask);
  133.                     if (HiWord(menuChoice) != 0 && IsOurWindow(theWindow))
  134.                         DoCommand(theWindow, menuChoice);
  135.                     else if (IsOurWindow(theWindow))
  136.                         DoWindowKeyDown((DocumentPtr) theWindow);
  137.                     else {
  138.                         SysBeep(10);
  139.                     }
  140.                 }
  141.             }
  142.             else if (IsOurWindow(theWindow))
  143.                 DoWindowKeyDown((DocumentPtr) theWindow);
  144.             else {
  145.                 SysBeep(10);
  146.             }
  147.             break;
  148.         case mouseDown:
  149.             DoMouseEvent();
  150.             break;
  151.         case updateEvt:
  152.             theWindow = (WindowPtr) EVENT.message;
  153.             GetPort(&savePort);
  154.             SetPort(theWindow);
  155.             BeginUpdate(theWindow);
  156.             EraseRect(&theWindow->portRect);
  157.             TextFont(applFont);
  158.             TextSize(kApplFontSize);
  159.             TextFace(normal);
  160.             PenNormal();
  161.             UpdateControls(theWindow, theWindow->visRgn);
  162.             if (IsOurWindow(theWindow))
  163.                 UpdateDocumentWindow((DocumentPtr) theWindow);
  164.             EndUpdate(theWindow);
  165.             SetPort(savePort);
  166.             break;
  167.         case activateEvt:
  168.             theWindow = (WindowPtr) EVENT.message;
  169.             isActivating = ((EVENT. modifiers & activeFlag) != 0);
  170.             goto activateEvent;
  171.             break;
  172.         case osEvt:
  173.             switch (((unsigned long) EVENT.message) >> 24) {
  174.             case mouseMovedMessage:
  175.                 break;
  176.             case suspendResumeMessage:
  177.                 isActivating = ((EVENT.message & 0x01) != 0);
  178. activateEvent:    if (isActivating) {
  179.                     /*
  180.                      * Activate this window. Note that activate events
  181.                      * define theWindow from the EventMessage, while
  182.                      * suspend/resume events use the pre-set FrontWindow.
  183.                      */
  184.                     SelectWindow(theWindow);
  185.                     SetPort(theWindow);
  186.                     (void) TEFromScrap();        /* Not really needed    */
  187.                     if (IsOurWindow(theWindow)) {
  188.                         ActivateDocumentWindow(
  189.                             (DocumentPtr) theWindow,
  190.                             isActivating
  191.                         );
  192.                     }
  193.                     else {
  194.                         /*
  195.                          * Hmm, can it be a desk accessory window?
  196.                          */
  197.                     }
  198.                     gInForeground = isActivating;
  199.                     gUpdateMenusNeeded = TRUE;
  200.                 }
  201.                 break;                        /* Break from osEvt switch    */
  202.             }
  203.             break;                            /* Break from event switch    */
  204.         }
  205. }
  206.  
  207. /*
  208.  * Look at all of the windows. If any are ours, drain its audit record.
  209.  */
  210. void
  211. ProcessAudit(void)
  212. {
  213.         register WindowPtr                thisWindow;
  214.         register WindowPeek                nextWindow;
  215.         
  216.         thisWindow = FrontWindow();
  217.         while (thisWindow != NULL) {
  218.             nextWindow = ((WindowPeek) thisWindow)->nextWindow;
  219.             if (IsOurWindow(thisWindow))
  220.                 ProcessAuditDocument((DocumentPtr) thisWindow);
  221.             thisWindow = (WindowPtr) nextWindow;
  222.         }
  223. }
  224.  
  225.  
  226. /*
  227.  * DoMouseEvent: the user clicked on something. Handle application-wide
  228.  * processing here, or call a DisplayAudit function for specific action.
  229.  */
  230. void
  231. DoMouseEvent(void)
  232. {
  233.         WindowPtr                    theWindow;
  234.         short                        whichPart;
  235.         
  236.         whichPart = FindWindow(EVENT.where, &theWindow);
  237.         if (theWindow == NULL)
  238.             theWindow = FrontWindow();
  239.         if (whichPart == inMenuBar && IsOurWindow(theWindow) == FALSE)
  240.             theWindow = FrontWindow();
  241.         switch (whichPart) {
  242.         case inDesk:
  243.             break;
  244.         case inMenuBar:
  245.             InitCursor();
  246.             DoCommand(theWindow, MenuSelect(EVENT.where));
  247.             break;
  248.         case inDrag:
  249.             DragWindow(theWindow, EVENT.where, &qd.screenBits.bounds);
  250.             break;
  251.         case inGoAway:
  252.             if (TrackGoAway(theWindow, EVENT.where)) {
  253.                 if (IsOurWindow(theWindow)) {
  254.                     DisposeDocumentWindow((DocumentPtr) theWindow);
  255.                     if (gOpenWindowCount <= 0)
  256.                         gQuitNow = TRUE;
  257.                 }
  258.                 else {
  259.                     SysBeep(10);
  260.                 }
  261.             }
  262.             break;
  263.         case inZoomIn:
  264.         case inZoomOut:
  265.             if (IsOurWindow(theWindow)
  266.              && TrackBox(theWindow, EVENT.where, whichPart)) {
  267.                  DoZoomWindow(theWindow, whichPart);
  268.                  goto resizeWindow;
  269.             }
  270.             break;
  271.         case inGrow:
  272.             if (IsOurWindow(theWindow)
  273.              && DoGrowWindow(theWindow, kMinWindowWidth, kMinWindowHeight)) {
  274. resizeWindow:    DecorateWindow((DocumentPtr) theWindow);
  275.                 gParameterUpdateNeeded = TRUE;
  276.             }
  277.             break;
  278.         case inContent:
  279.             if (theWindow != FrontWindow()) {
  280.                 SelectWindow(theWindow);
  281.                 SetPort(theWindow);
  282.             }
  283.             else if (IsOurWindow(theWindow))
  284.                 DoContentClick((DocumentPtr) theWindow);
  285.             else {
  286.                 /* Nothing happens here */
  287.             }
  288.             break;
  289.         default:
  290.             break;
  291.         }
  292. }
  293.  
  294. /*
  295.  * A menu choice.
  296.  */
  297. void
  298. DoCommand(
  299.         WindowPtr                        theWindow,
  300.         long                            menuChoice
  301.     )
  302. {
  303.         short                            menuItem;
  304.         Str255                            menuText;
  305.         GrafPtr                            savePort;
  306.         
  307.         menuItem = LoWord(menuChoice);
  308.         switch (HiWord(menuChoice)) {
  309.         case MENU_Apple:
  310.             if (menuItem == kAppleAbout)
  311.                 DoAbout();
  312.             else {
  313.                 GetItem(gAppleMenu, menuItem, menuText);
  314.                 EnableEditMenu();
  315.                 GetPort(&savePort);
  316.                 OpenDeskAcc(menuText);
  317.                 SetPort(savePort);
  318.                 gUpdateMenusNeeded = TRUE;
  319.             }
  320.             break;
  321.         case MENU_File:
  322.             switch (menuItem) {
  323.             case kFileNewWindow:
  324.                 MakeDocumentWindow();
  325.                 break;
  326.             case kFileCloseWindow:
  327.                 if (IsOurWindow(theWindow)) {
  328.                     DisposeDocumentWindow((DocumentPtr) theWindow);
  329.                     if (gOpenWindowCount <= 0)
  330.                         gQuitNow = TRUE;
  331.                 }
  332.                 break;
  333.             case kFileSaveAs:
  334.                 if (IsOurWindow(theWindow))
  335.                     DoDocumentSaveAs((DocumentPtr) theWindow);
  336.                 break;
  337.             case kFileCloseFile:
  338.                 if (IsOurWindow(theWindow))
  339.                     DoDocumentCloseFile((DocumentPtr) theWindow);
  340.                 break;
  341.             case kFilePageSetup:
  342.                 DoPageSetup();
  343.                 break;
  344.             case kFilePrint:
  345.                 if (IsOurWindow(theWindow)) {
  346.                     short                fontNumber;        /* Hack!                */
  347.                     short                fontSize;
  348.                     
  349.                     GetFNum("\pCourier", &fontNumber);
  350.                     if (fontNumber != 0)
  351.                         fontSize = 10;
  352.                     else {
  353.                         fontNumber = monaco;
  354.                         fontSize = 9;
  355.                     }
  356.                     PrintLog(
  357.                         ((DocumentPtr) theWindow)->logListHandle,
  358.                         gPrintHandle,
  359.                         fontNumber,
  360.                         fontSize
  361.                     );
  362.                 }
  363.                 break;
  364.             case kFileTimeStamp:
  365.                 if (IsOurWindow(theWindow))
  366.                     TimeStamp((DocumentPtr) theWindow);
  367.                 break;
  368.             case kFileQuit:
  369.                 gQuitNow = TRUE;
  370.                 break;
  371.             default:
  372.                 SysBeep(10);
  373.                 break;
  374.             }
  375.             break;
  376.         case MENU_Edit:
  377.             if (SystemEdit(menuItem - 1) == FALSE) {
  378.                 if (IsOurWindow(theWindow))
  379.                     DoDocumentEditMenu((DocumentPtr) theWindow, menuItem);
  380.                 else {
  381.                     SysBeep(10);
  382.                 }
  383.             }
  384.             break;
  385.         }
  386.         HiliteMenu(0);
  387.         gUpdateMenusNeeded = TRUE;
  388. }        
  389.  
  390. void
  391. DoAbout(void)
  392. {
  393.         GrafPtr                            savePort;
  394.         DialogPtr                        dialog;
  395.         short                            dialogItem;
  396.         DialogTHndl                        dialogHdl;
  397.         Rect                            box;
  398.         short                            leftEdge;
  399.         short                            topEdge;
  400.  
  401.         /*
  402.          * Center the dialog
  403.          */
  404.         dialogHdl = (DialogTHndl) GetResource('DLOG', DLOG_About);
  405.         if (dialogHdl != NULL) {
  406.             box = (**dialogHdl).boundsRect;                /* Dialog shape            */
  407.             leftEdge = (width(qd.screenBits.bounds) - width(box)) / 2;
  408.             topEdge = 
  409.                 ((height(qd.screenBits.bounds) - GetMBarHeight()) / 3)
  410.                 + GetMBarHeight()
  411.                 - (height(box) / 3);
  412.             OffsetRect(&(**dialogHdl).boundsRect, -box.left, -box.top);
  413.             OffsetRect(&(**dialogHdl).boundsRect,  leftEdge,  topEdge);
  414.         }
  415.         GetPort(&savePort);
  416.         dialog = GetNewDialog(DLOG_About, NULL, (WindowPtr) -1L);
  417.         if (dialog != NULL) {
  418.             ShowWindow(dialog);
  419.             SetPort(dialog);
  420.             ModalDialog(NULL, &dialogItem);
  421.             DisposDialog(dialog);
  422.             SetPort(savePort);
  423.         }
  424. }
  425.  
  426. void
  427. AdjustMenus(void)
  428. {
  429.         register WindowPtr                theWindow;
  430.         register DocumentPtr            documentPtr;
  431.         
  432.         theWindow = FrontWindow();
  433.         EnableItem(gFileMenu, kFileQuit);
  434.         EnableItem(gFileMenu, kFileNewWindow);
  435.         if (IsOurWindow(theWindow)) {
  436.             EnableItem(gFileMenu, kFileCloseWindow);
  437.             documentPtr = (DocumentPtr) theWindow;
  438.             EnableItem(gFileMenu, kFilePageSetup);
  439.             EnableItem(gFileMenu, kFilePrint);
  440.             if (DOC.auditPtr != NULL)
  441.                 EnableItem(gFileMenu, kFileTimeStamp);
  442.             else {
  443.                 DisableItem(gFileMenu, kFileTimeStamp);
  444.             }
  445.             if (DOC.logFileRefNum == 0) {
  446.                 EnableItem(gFileMenu, kFileSaveAs);
  447.                 DisableItem(gFileMenu, kFileCloseFile);
  448.             }
  449.             else {
  450.                 EnableItem(gFileMenu, kFileCloseFile);
  451.                 DisableItem(gFileMenu, kFileSaveAs);
  452.             }
  453.             CheckItem(gFileMenu, kFileSaveAs, DOC.logFileRefNum != 0);
  454.             AdjustDocumentEditMenu((DocumentPtr) theWindow);
  455.         }
  456.         else {
  457.             EnableEditMenu();
  458.         }
  459. }
  460.  
  461. void
  462. EnableEditMenu(void)
  463. {
  464.         EnableItem(gEditMenu, kEditUndo);
  465.         EnableItem(gEditMenu, kEditCut);
  466.         EnableItem(gEditMenu, kEditCopy);
  467.         EnableItem(gEditMenu, kEditPaste);
  468.         EnableItem(gEditMenu, kEditClear);
  469. }
  470.  
  471. void
  472. DeleteAllWindows(void)
  473. {
  474.         WindowPtr                        thisWindow;
  475.         WindowPeek                        nextWindow;
  476.         
  477.         thisWindow = FrontWindow();
  478.         while (thisWindow != NULL) {
  479.             nextWindow = ((WindowPeek) thisWindow)->nextWindow;
  480.             if (IsOurWindow(thisWindow))
  481.                 DisposeDocumentWindow((DocumentPtr) thisWindow);
  482.             thisWindow = (WindowPtr) nextWindow;
  483.         }
  484. }
  485.  
  486. void
  487. InitMacintosh(void)
  488. {
  489.         int                                i;
  490.         long                            response;
  491.         
  492.         MaxApplZone();
  493.         InitGraf(&qd.thePort);
  494.         InitFonts();
  495.         InitWindows();
  496.         InitMenus();
  497.         TEInit();
  498.         InitDialogs(NULL);
  499.         HNoPurge((Handle) GetCursor(watchCursor));
  500.         SetCursor(*GetCursor(watchCursor));
  501.         for (i = 0; i < 3; i++)
  502.             EventAvail(everyEvent, &EVENT);
  503.         if (Gestalt(gestaltQuickdrawVersion, &response) == noErr
  504.          && response >= gestalt8BitQD)
  505.              gHasColorQuickDraw = TRUE;
  506.  
  507. }
  508.  
  509. void
  510. InitApplication(void)
  511. {
  512.         Handle                            menuBarHdl;
  513.         
  514.         menuBarHdl = GetNewMBar(MBAR_MenuBar);
  515.         FailNIL(menuBarHdl, kErrInitialization);
  516.         SetMenuBar(menuBarHdl);
  517.         gAppleMenu = GetMHandle(MENU_Apple);
  518.         FailNIL(gAppleMenu, kErrInitialization);
  519.         gFileMenu = GetMHandle(MENU_File);
  520.         FailNIL(gFileMenu, kErrInitialization);
  521.         gEditMenu = GetMHandle(MENU_Edit);
  522.         FailNIL(gEditMenu, kErrInitialization);
  523.         AddResMenu(GetMHandle(MENU_Apple), 'DRVR');
  524.         DrawMenuBar();
  525.         gFontMenu = GetMenu(MENU_Font);
  526.         FailNIL(gFontMenu, kErrInitialization);
  527.         AddResMenu(gFontMenu, 'FONT');
  528.         gFontSizeMenu = GetMenu(MENU_FontSize);
  529.         FailNIL(gFontSizeMenu, kErrInitialization);
  530.         OpenAuditParameterFile();
  531.         ReadAuditParameters();
  532.         InitCursor();
  533. }
  534.  
  535. /*
  536.  * This displays an error message and its accompaning text. It should be
  537.  * redone to use a message index (to an STR# resource.
  538.  */
  539. void    
  540. ErrorAlert(
  541.         OSErr                            status,
  542.         short                            messageIndex,
  543.         Boolean                            fatal
  544.     )
  545. {
  546.         Str15                            statusText;
  547.         short                            alertID;
  548.         Str255                            errorMsg;
  549.         
  550.         NumToString(status, statusText);
  551.         GetIndString(errorMsg, STRN_Messages, messageIndex);
  552.         alertID = (fatal) ? ALRT_Fatal : ALRT_NonFatal;
  553.         ParamText(statusText, errorMsg, "\p", "\p");
  554.         StopAlert(alertID, NULL);
  555. }
  556.  
  557. void
  558. DoPageSetup(void)
  559. {
  560.         PrOpen();
  561.         if (PrError() == noErr) {
  562.             if (gPrintHandle == NULL) {
  563.                 gPrintHandle = (THPrint) NewHandle(sizeof (TPrint));
  564.                 if (gPrintHandle != NULL)
  565.                     PrintDefault(gPrintHandle);
  566.             }
  567.             if (gPrintHandle != NULL)
  568.                 (void) PrStlDialog(gPrintHandle);
  569.             PrClose();
  570.         }
  571. }
  572.